<template name="addCard">
<view class="bgzd">
	<view class="form">
		<view class="form_top">
			<text>客户开卡</text>
			<image @click="close" src="/static/img/none_close.png"></image>
		</view>
		<view class="view_input1">
		    <view class="view_input_title1">项目名称:</view>
			<picker class="view_input_input1" @change="bindCardIndex" :value="cardIndex" 
			:range="card" v-if="card.length > 0"
			range-key="name">
				<view>{{card[cardIndex]['name']}}</view>
			</picker>
		</view>
		<view class="view_input1">
		    <view class="view_input_title1">使用次数:</view>
		    <input class="view_input_input1" type="number" v-model="form.num"
		    placeholder-class="placeholderClass" placeholder="不限次数可不填"/>
		</view>
		<view class="view_input1">
		    <view class="view_input_title1">应收金额:</view>
		    <input class="view_input_input1" type="number" @input="give" v-model="form.money"
		    placeholder-class="placeholderClass" placeholder="请输入应收金额"/>
		</view>
		<view class="view_input1">
		    <view class="view_input_title1">账户抵扣:</view>
		    <input class="view_input_input" type="number" @input="give" v-model="form.offset_money"
		    placeholder-class="placeholderClass" placeholder="请输入抵扣金额"/>
		</view>
		<view class="view_input1">
		    <view class="view_input_title1">代金券:</view>
		    <input class="view_input_input" type="number" @input="give" v-model="form.give_money"
		    placeholder-class="placeholderClass" placeholder="请输入代金券金额"/>
		</view>
		<view class="view_input1">
		    <view class="view_input_title1">实收金额:</view>
		    <input class="view_input_input" type="number" v-model="form.pay_money" disabled
		    placeholder-class="placeholderClass" placeholder="请输入实收金额"/>
		</view>
		<button v-if="addRoleTrue" @click="add" class="strue">提交</button>
	</view>
</view>
</template>
<script>
import request from '@/util/request.js'
export default {
	name: "addCard",
	//属性
	props:['id'],
	data() {
		return {
			addRoleTrue:true,
			form:{
				card_id:0,
				offset_money:'',
				give_money:''
			},
			cardIndex:0,
			card:[]
		}
	},
	//组件生命周期
	created:function(e){
		this.getCard()
	},
	methods:{
		getCard:function(){
			var data = {
			    oa_id:uni.getStorageSync('oa_id')
			}
			var that = this
			request({
				url: 'manage/card/all',
				method: 'get',
				data:data,
				success: (res) => {
					that.card = res.data.data
					that.form.card_id = res.data.data[0].id
				},
			})
		},
		give:function(){
			if(this.form.money){
				var money = this.form.money
			}else{
				var money = 0
			}
			if(this.form.offset_money){
				var offset = this.form.offset_money
			}else{
				var offset = 0
			}
			if(this.form.give_money){
				var give = this.form.give_money
			}else{
				var give = 0
			}
			this.form.pay_money = parseFloat(money) - parseFloat(offset) - parseFloat(give)
		},
		close:function(){
			this.$emit('func',{'type':0,'model':'addCardShow'})
		},
		bindCardIndex:function(e){
			this.cardIndex = e.detail.value
			this.form.card_id = this.card[e.detail.value].id
		},
		add:function(){
			if(!this.form.money){
				uni.showToast({
					title: '请填写应收金额!!!',
					icon: "none", 
					position:'bottom',
				});
				return;
			}
			if(!this.form.num){
				this.form.num = 9999
			}
			this.form.oa_id = uni.getStorageSync('oa_id')
			this.form.member_id = this.id
			var that = this
			this.addRoleTrue = false
			request({
				url: 'manage/card/addOrder',
				method: 'post',
				data:this.form,
				success: (res) => {
					if(res.data.code == 1){
						uni.showToast({
							title: '购卡成功!!',
							icon: "success", 
							position:'bottom',
						});
						that.$emit('func',{'type':1,'model':'addCardShow'})
					}else{
						uni.showToast({
							title:res.data.msg,
							icon: "none", 
							position:'bottom',
						});
					}
				},
			})
		},
	}
}
</script>
<style>
.bgzd{
	position: fixed;
	top: 0;
	left: 0;
	bottom: 0;
	right: 0;
	width: 100%;
	height: 100%;
	background: rgba(0,0,0,0.4);
	z-index:999;
	padding-top:40%;
	padding-left:5%;
}
.form{
    padding-top:20rpx;
    width:90%;
    padding-bottom: 20rpx;
    background:#FFFFFF;
    position: fixed;
    z-index:9999;
	border-radius: 40rpx;
	padding-bottom: 50rpx;
}
.form_top{
    font-weight:bold;
    font-size:20px;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
	padding:15rpx 15rpx 15rpx 15rpx;
}
.form_top image{
    width:35rpx;height:35rpx;
    margin-right: 20rpx;
}
.strue{
    width:90%;height:70rpx;line-height:70rpx;
    background:#ff861a;color:#fff;
	margin-top: 20rpx;
	border-radius: 30rpx;
}
</style>